local zhiheng = fk.CreateSkill{
  name = "klo__zhiheng",
}

Fk:loadTranslationTable{
  ["klo__zhiheng"] = "会玩",
  [":klo__zhiheng"] = "出牌阶段限一次，你可以弃置任意张牌，然后获得牌堆中等量张指定牌名的牌。若你以此法弃置了所有手牌，你多从牌堆中获得一张指定牌名的牌。",

  ["#klo__zhiheng"] = "会玩：弃置任意张牌并获得牌堆中等量张指定牌名的牌，若弃置了所有手牌，你多获得一张指定牌名的牌",
  ["#klo__zhiheng-choose"] = "会玩：请选择一个牌名的牌从牌堆中获得（第%arg个，共%arg2个）",

  ["$klo__zhiheng1"] = "制天下之变，衡左右制心。",
  ["$klo__zhiheng2"] = "雄略并江夏，制衡稳吴中。",
}

local U = require "packages.utility.utility"

zhiheng:addEffect("active", {
  anim_type = "drawcard",
  min_card_num = 1,
  target_num = 0,
  prompt = "#klo__zhiheng",
  card_filter = function(self, player, to_select, selected)
    return not player:prohibitDiscard(to_select)
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(zhiheng.name, Player.HistoryPhase) < 1
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local hand = player:getCardIds("h")
    local more = #hand > 0
    for _, id in ipairs(hand) do
      if not table.contains(effect.cards, id) then
        more = false
        break
      end
    end
    room:throwCard(effect.cards, zhiheng.name, player, player)
    if player.dead then return end
    local num = #effect.cards + (more and 1 or 0)
    local result = {}
    for i = 1, num do
      local choices = {}
      local temp = table.simpleClone(result)
      for _, id in ipairs(Fk:currentRoom().draw_pile) do
        local name = Fk:getCardById(id).name
        table.insertIfNeed(choices, name)
        if table.contains(temp, name) then
          table.removeOne(choices, name)
          table.removeOne(temp, name)
        end
      end
      if #choices == 0 then break end
      local name = U.askForChooseCardNames(room, player, choices, 1, 1, zhiheng.name, "#klo__zhiheng-choose:::" .. i..":"..num, Fk:getAllCardNames("btde"))
      table.insertTable(result, name)
    end
    if #result > 0 then
      local toDraw = {}
      for i = #room.draw_pile, 1, -1 do
        local card = Fk:getCardById(room.draw_pile[i])
        if table.contains(result, card.name) then
          table.removeOne(result, card.name)
          table.insert(toDraw, card.id)
        end
      end
      if #toDraw > 0 then
        room:obtainCard(player, toDraw, false, fk.ReasonPrey, player, zhiheng.name)
      end
    end
  end
})


zhiheng:addAI({
  think = function(self, ai)
    local player = ai.player
    local cards = ai:getEnabledCards(".|.|.|hand|.|.|.")

    cards = ai:getChoiceCardsByKeepValue(cards, #cards, function(value) return value < 45 end)

    return { cards = cards }, ai:getBenefitOfEvents(function(logic)
      logic:throwCard(cards, self.skill.name, player, player)
      logic:drawCards(player, #cards, self.skill.name)
    end)
  end,
})


return zhiheng
